package com.china08.yxyapi.api.manage.tec.teaching.lecture.prepare;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.SystemConstant;
import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLectureCatalog;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.lecture.YxyLectureCatalogRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;
import com.china08.yxyapi.util.LectureUtil;
import com.china08.yxyapi.util.YxUserPointsUtilV3;

import io.swagger.annotations.ApiOperation;

/**
 * 老师 ---备授课--创建成品备课包
 * 
 */
@CrossOrigin
@RestController
public class LectureCrtPrePackController extends YxyApiBaseController {

    @Autowired
    private YxyLectureCatalogRepository yxyLectureCatalogRepository;

    @Autowired
    private YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @Autowired
    private LectureUtil lectureUtil;

    @Autowired
    private YxUserPointsUtilV3 yxUserPointsUtilV3;

    @ApiOperation(value = "老师 ---备授课--创建成品备课包")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/prepare/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel req,
            RespModel respModel) {

        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        Assert.notNull(StringUtils.defaultIfBlank(userId, null), "用户id必须");
        Assert.notNull(StringUtils.defaultIfBlank(schoolId, null), "学校id必须");

        YxyLectureCatalog yxyLectureCatalog = yxyLectureCatalogRepository.findById(req.catalogId);
        YxyLecturePrePack yxyLecturePrePack = new YxyLecturePrePack();
        yxUserPointsUtilV3.createBackpack(userId);
        String userNick = getProfile(request, response).getUserNick();
        String schoolNick = getProfile(request, response).getSchoolName();
        yxyLecturePrePack.setUserId(userId);
        yxyLecturePrePack.setSchoolId(schoolId);
        yxyLecturePrePack.setUserNick(userNick);
        yxyLecturePrePack.setSchoolNick(schoolNick);
        yxyLecturePrePack.setPhaseId(yxyLectureCatalog.getPhaseId());
        yxyLecturePrePack.setPhaseName(yxyLectureCatalog.getPhaseName());
        yxyLecturePrePack.setSubjectId(yxyLectureCatalog.getSubjectId());
        yxyLecturePrePack.setSubjectName(yxyLectureCatalog.getSubjectName());
        yxyLecturePrePack.setVersionId(yxyLectureCatalog.getVersionId());
        yxyLecturePrePack.setVersionName(yxyLectureCatalog.getVersionName());
        yxyLecturePrePack.setSemesterId(yxyLectureCatalog.getSemesterId());
        yxyLecturePrePack.setSemesterName(yxyLectureCatalog.getSemesterName());
        yxyLecturePrePack.setBookcontentsId(req.bookcontentsId);
        yxyLecturePrePack.setBookcontentsName(req.bookContentsName);
        yxyLecturePrePack.setPackName(req.packName);
        yxyLecturePrePack.setStatus(1);
        yxyLecturePrePack.setIsDelete(0);
        yxyLecturePrePack.setAuditStats(0);
        yxyLecturePrePack.setPackSize(req.packSize);
        // 增加备课包的省市地区代码 2017.03.27 王慧超
        yxyLecturePrePack.setAreaIdCity(getProfile(request, response).getSchoolAreaIdCity());
        yxyLecturePrePack.setAreaIdDistrict(getProfile(request, response).getSchoolAreaIdDistrict());
        yxyLecturePrePack.setAreaIdProvince(getProfile(request, response).getSchoolAreaIdProvince());
        // 增加是否为云校使用的id.判断是否为云校的 备课包
        yxyLecturePrePack.setIsYx("".equals(SystemConstant.YUNXIAO_SCHOOL_ID) ? 1 : 0);
        // 增加共享模式,如果没有值,默认为共享 2017.03.27 王慧超
        yxyLecturePrePack.setShare(req.share != null ? req.share : 1);

        List<com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res> resList = new ArrayList<>();
        Integer orderNum = 1;
        for (Res res : req.resources) {
            com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res s = new YxyLecturePrePack().new Res();
            s.setCourseType(res.courseType);
            s.setFormatId(res.formatId);
            s.setResourceName(res.resourceName);
            s.setResourceId(res.resourceId);
            s.setResourceUrl(res.resourceUrl);
            Long fileSize = res.fileSize;
            if (null != fileSize) {
                s.setFileSize(Integer.valueOf(fileSize + "") * 1024);
            } else {
                s.setFileSize(0);
            }
            s.setOrderNum(orderNum);
            List<Integer> useCheckbox = res.useCheckbox;
            if (CollectionUtils.isNotEmpty(useCheckbox)) {
                for (Integer integer : useCheckbox) {
                    if (1 == integer) {
                        s.setUsesTeaching(1);
                    }
                    if (2 == integer) {
                        s.setUsesProview(1);
                    }
                    if (3 == integer) {
                        s.setUsesHomework(1);
                    }
                    if (4 == integer) {
                        s.setUsesTest(1);
                    }
                }
            }
            orderNum++;
            resList.add(s);
        }
        yxyLecturePrePack.setResources(resList);
        yxyLecturePrePack = yxyLecturePrePackRepository.save(yxyLecturePrePack);
        if (req.share == 1) {
            lectureUtil.save(getProfile(request, response), 1, "ss", "dd", yxyLecturePrePack.getCreatedDate());
        }
        respModel = new RespModel();
        respModel.setMessage("备授课-成品备课包创建成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String catalogId;
        /**
         * 目录id
         */
        public String bookcontentsId;

        public String bookContentsName;

        public String packName;

        public Integer share;
        // 备课包大小
        public Long packSize;
        /**
         * 资源id
         */
        public List<Res> resources;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(catalogId, null), "目录不能为空");
            Assert.notNull(StringUtils.defaultIfBlank(packName, null), "包名不能为空");
            Assert.notNull(StringUtils.defaultIfBlank(bookcontentsId, null), "章节不能为空");
            Assert.notEmpty(resources, "资源必须大于1个");
        }

    }

    protected static class Res {
        public String resourceId;
        public String resourceName;
        public String resourceUrl;
        public String formatId;
        public List<Integer> useCheckbox;
        // 0 试题 1资源
        public int courseType = 1;
        // 资源大小
        public Long fileSize;
    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
